package com.itany.boot.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.boot.constant.DictConstant;
import com.itany.boot.constant.StatusConstant;
import com.itany.boot.dao.CourseTypeMapper;
import com.itany.boot.entity.CourseType;
import com.itany.boot.entity.CourseTypeExample;
import com.itany.boot.exception.CourseTypeExistException;
import com.itany.boot.exception.LevelErrorException;
import com.itany.boot.exception.RequestParameterErrorException;
import com.itany.boot.service.CourseTypeService;
import com.itany.boot.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @date 2025/10/13
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CourseTypeServiceImpl implements CourseTypeService {

    @Autowired
    private CourseTypeMapper courseTypeMapper;


    @Override
    public PageInfo<CourseType> findChileByParentId(String parentId, String pageNo, String pageSize) {
        if(ParameterUtil.isNull(pageNo)){
            pageNo = DictConstant.COURSE_DEFAULT_PAGE_NO;
        }
        if(ParameterUtil.isNull(pageSize)){
            pageSize = DictConstant.COURSE_DEFAULT_PAGE_SIZE;
        }

        CourseTypeExample example = new CourseTypeExample();
        if(!ParameterUtil.isNull(parentId)){
            example.or()
                    .andParentIdEqualTo(Integer.parseInt(parentId));
        }else{
            example.or()
                    .andParentIdIsNull();
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        List<CourseType> types = courseTypeMapper.selectByExample(example);
        PageInfo<CourseType> typePage = new PageInfo<>(types);
        return typePage;
    }

    @Override
    public void addChileType(String typeName, String parentId) throws RequestParameterErrorException, CourseTypeExistException, LevelErrorException {
        if(ParameterUtil.isNull(typeName) || ParameterUtil.isNull(parentId)){
            throw new RequestParameterErrorException("请求参数有误");
        }

        CourseTypeExample example = new CourseTypeExample();
        example.or()
                .andTypeNameEqualTo(typeName);
        List<CourseType> types = courseTypeMapper.selectByExample(example);
        if(!types.isEmpty()){
            throw new CourseTypeExistException("该商品类型已存在");
        }

        int level = getLevel(parentId);
        if(level >= 3){
            throw new LevelErrorException("已经达到最大层数三层");
        }

        CourseType chileType = new CourseType();
        chileType.setTypeName(typeName);
        chileType.setParentId(Integer.parseInt(parentId));
        chileType.setStatus(DictConstant.COURSE_TYPE_STATUS_ENABLED);
        chileType.setLevel(level+1);
        courseTypeMapper.insertSelective(chileType);
    }

    @Override
    public void modifyChileStatus(String parentId, String chileStatus) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(parentId) || ParameterUtil.isNull(chileStatus)) {
            throw new RequestParameterErrorException("请求参数有误");
        }

        Integer status;
        status = Integer.parseInt(chileStatus);

        CourseTypeExample example = new CourseTypeExample();
        example.or()
                .andParentIdEqualTo(Integer.parseInt(parentId));
        List<CourseType> types = courseTypeMapper.selectByExample(example);

        if(status == DictConstant.COURSE_TYPE_STATUS_DISABLED){
            for(CourseType type : types){
                CourseType chileType = new CourseType();
                chileType.setId(type.getId());
                chileType.setStatus(DictConstant.COURSE_TYPE_STATUS_DISABLED);
                courseTypeMapper.updateByPrimaryKeySelective(chileType);

                modifyChileStatus(type.getId().toString(), chileStatus);
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public int getLevel(String id) throws RequestParameterErrorException {
        CourseType type = findById(id);
        if(type == null){
            return 1;
        }
        if(type.getLevel() != null){
            return type.getLevel();
        }
        Integer parentId = type.getParentId();
        if(parentId == null){
            return 1;
        }else{
            return getLevel(parentId.toString()) + 1;
        }
    }

    @Override
    public void addCourseType(String name) throws RequestParameterErrorException, CourseTypeExistException {
        if(ParameterUtil.isNull(name)){
            throw new RequestParameterErrorException("请求参数有误");
        }

        CourseTypeExample example = new CourseTypeExample();
        example.or()
                .andTypeNameEqualTo(name);
        List<CourseType> types = courseTypeMapper.selectByExample(example);
        if(!types.isEmpty()){
            throw new CourseTypeExistException("该课程类型已经存在");
        }

        CourseType type = new CourseType();
        type.setTypeName(name);
        type.setStatus(DictConstant.COURSE_TYPE_STATUS_ENABLED);
        courseTypeMapper.insertSelective(type);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<CourseType> findPage(String pageNo, String pageSize) {
        if(ParameterUtil.isNull(pageNo)){
            pageNo = DictConstant.COURSE_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.COURSE_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        List<CourseType> types = courseTypeMapper.selectByExample(new CourseTypeExample());
        PageInfo<CourseType> typePage = new PageInfo<>(types);
        return typePage;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public CourseType findById(String id) throws RequestParameterErrorException {
        if(ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        return courseTypeMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public void modifyName(String id, String typeName) throws RequestParameterErrorException, CourseTypeExistException {
        if(ParameterUtil.isNull(id) || ParameterUtil.isNull(typeName)){
            throw new RequestParameterErrorException("请求参数有误");
        }

        CourseTypeExample example = new CourseTypeExample();
        example.or()
                .andTypeNameEqualTo(typeName)
                .andIdNotEqualTo(Integer.parseInt(id));
        List<CourseType> types = courseTypeMapper.selectByExample(example);
        if(!types.isEmpty()){
            throw new CourseTypeExistException("该类型课程已经存在");
        }

        CourseType type = new CourseType();
        type.setId(Integer.parseInt(id));
        type.setTypeName(typeName);
        courseTypeMapper.updateByPrimaryKeySelective(type);
    }

    @Override
    public void modifyStatus(String id, String status) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(status)) {
            throw new RequestParameterErrorException("请求参数错误");
        }
        CourseType type = new CourseType();
        type.setId(Integer.parseInt(id));
        type.setStatus(Integer.parseInt(status));
        if(type.getStatus() == StatusConstant.USER_STATUS_ENABLED){
            type.setStatus(StatusConstant.USER_STATUS_DISABLED);
        }else {
            type.setStatus(StatusConstant.USER_STATUS_ENABLED);
        }
        courseTypeMapper.updateByPrimaryKeySelective(type);
    }
    
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<CourseType> selectByParentId(String parentId) {
        if (ParameterUtil.isNull(parentId)) {
            return courseTypeMapper.selectByParentId(null);
        }
        return courseTypeMapper.selectByParentId(Integer.parseInt(parentId));
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<CourseType> findEnabled() {
        CourseTypeExample example = new CourseTypeExample();
        example.or()
                .andStatusEqualTo(DictConstant.COURSE_TYPE_STATUS_ENABLED);
        return courseTypeMapper.selectByExample(example);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<CourseType> findAll() {
        return courseTypeMapper.selectByExample(new CourseTypeExample());
    }
}
